/**
 * 节流函数
 */
export function throttle<T, R>(fn: (...args: T[]) => R, delay: number): (this: unknown, ...args: T[]) => void {
    let timer: NodeJS.Timeout | null;
    return function (this: unknown, ...args: T[]) {
        if (timer) {
            return;
        }
        timer = setTimeout(() => {
            fn.apply(this, args);
            timer = null;
        }, delay);
    };
}

/**
 * 防抖函数
 * @param fn
 * @param delay
 * @returns
 */
export function debounce<T, R>(fn: (...args: T[]) => R, delay: number): (this: unknown, ...args: T[]) => void {
    let timer: NodeJS.Timeout | null;
    return function (this: unknown, ...args: T[]) {
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
            fn.apply(this, args);
            timer = null;
        }, delay);
    };
}

/**
 * 生成指定长度随机字符串
 * @param length
 * @returns
 */
export function randomString(length: number): string {
    const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    let result = '';
    for (let i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
    return result;
}

/**
 * 生成随机的十六进制颜色
 * @returns
 */
export function randomColor(): string {
    return (
        '#' +
        Math.floor(Math.random() * 0xffffff)
            .toString(16)
            .padEnd(6, '0')
    );
}

export const Colors = [
    '#ffb7dd',
    '#ff88c2',
    '#ff44aa',
    '#ff0088',
    '#ffcccc',
    '#ff8888',
    '#ff3333',
    '#ff0000',
    '#ffc8b4',
    '#ffa488',
    '#ff7744',
    '#ff5511',
    '#ffddaa',
    '#ffbb66',
    '#ffaa33',
    '#ff8800',
    '#ffee99',
    '#ffdd55',
    '#ffcc22',
    '#ffbb00',
    '#ffffbb',
    '#ffff77',
    '#ffff33',
    '#ffff00',
    '#eeffbb',
    '#ddff77',
    '#ccff33',
    '#bbff00',
    '#99dd00',
    '#ccff99',
    '#bbff66',
    '#99ff33',
    '#77ff00',
    '#66dd00',
    '#99ff99',
    '#66ff66',
    '#33ff33',
    '#00ff00',
    '#00dd00',
    '#bbffee',
    '#77ffcc',
    '#33ffaa',
    '#00ff99',
    '#00dd77',
    '#aaffee',
    '#77ffee',
    '#33ffdd',
    '#00ffcc',
    '#99ffff',
    '#66ffff',
    '#33ffff',
    '#00ffff',
    '#99ffff',
    '#66ffff',
    '#cceeff',
    '#77ddff',
    '#33ccff',
    '#00bbff',
    '#ccddff',
    '#99bbff',
    '#5599ff',
    '#0066ff ',
    '#ccccff',
    '#9999ff',
    '#ccbbff',
];

export function randomColorById(id: string | number): string {
    if (typeof id === 'string') id = parseInt(id);
    return Colors[(id * 7) % Colors.length];
}

/**
 * 验证用户名格式（允许中文字符）,最短2位，最长20位
 * @param username
 * @returns
 */
export function validateUsername(username: string): boolean {
    const reg = /^[\u4e00-\u9fa5_a-zA-Z0-9]{2,20}$/;
    return reg.test(username);
}

/**
 * 验证密码格式，最短6位，最长18位
 * @param password
 */
export function validatePassword(password: string): boolean {
    return /^[a-zA-Z0-9_-]{6,18}$/.test(password);
}

/**
 * 验证邮箱格式
 * @param email
 * @returns
 */
export function validateEmail(email: string): boolean {
    return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(email);
}
